//
//  DBManager.m
//  ManStoreDemo
//
//  Created by qianfeng01 on 15/7/21.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "DBManager.h"
#import "DetaiModel.h"
@implementation DBManager
{
    FMDatabase *_database;
}
+(DBManager *)sharedManager{
    static DBManager *manager=nil;
    @synchronized(self){
        if (manager==nil) {
            manager=[[self alloc] init];
        }
    }
    return manager;
}

-(id)init{
    if (self=[super init]) {
        NSString *filePath=[self getFileFullPathWithFileName:@"store.db"];
        _database=[[FMDatabase alloc] initWithPath:filePath];
        if ([_database open]) {
            NSLog(@"数据库打开成功");
            [self createTable];
        }else{
            NSLog(@"database open failed:%@",_database.lastErrorMessage);
        }
    }
    return self;
}
-(void)createTable{
    NSString *sql = @"create table if not exists appInfo(serial integer  Primary Key Autoincrement,title Varchar(1024),coupon_price Varchar(1024),pic_url Varchar(1024),num_iid Varchar(1024),size Varchar(1024),item_id Varchar(1024))";
    BOOL isSuccess=[_database executeUpdate:sql];
    if (!isSuccess) {
        NSLog(@"createTable error:%@",_database.lastErrorMessage);
    }

   }
- (NSString *)getFileFullPathWithFileName:(NSString *)fileName{
    NSString *docPath=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSFileManager *fm=[NSFileManager defaultManager];
    if ([fm fileExistsAtPath:docPath]) {
        return [docPath stringByAppendingFormat:@"/%@",fileName];
        
    }else{
        NSLog(@"Documents不存在");
        return nil;
    }
}
-(void)insertModel:(id)model{
    DetaiModel *detailModel=(DetaiModel *)model;
    if ([self isExistForitem_id:detailModel.item_id]) {
        NSLog(@"this app has recorded");
        return;
    }
    NSString *sql=@"insert into appInfo(title,coupon_price,pic_url,num_iid,size,item_id) values(?,?,?,?,?,?)";
    BOOL isSuccess=[_database executeUpdate:sql,detailModel.title,detailModel.coupon_price,detailModel.pic_url,detailModel.num_iid,detailModel.size,detailModel.item_id];
    if (!isSuccess) {
        NSLog(@"insert error:%@",_database.lastErrorMessage);
    }
}
-(NSArray *)readModel{
    NSString *sql=@"select * from appInfo";
    FMResultSet *rs=[_database executeQuery:sql];
    NSMutableArray *arr=[NSMutableArray array];
    while ([rs next]) {
        DetaiModel *detailModel=[[DetaiModel alloc] init];
        detailModel.title=[rs stringForColumn:@"title"];
        detailModel.pic_url=[rs stringForColumn:@"pic_url"];
        detailModel.coupon_price=[rs stringForColumn:@"coupon_price"];
        detailModel.num_iid=[rs stringForColumn:@"num_iid"];
        detailModel.size=[rs stringForColumn:@"size"];
        detailModel.item_id=[rs stringForColumn:@"item_id"];
        [arr addObject:detailModel];
    }
    return arr;
}
- (void)deleteModelForNum_iid:(NSString *)num_iid  {
    NSString *sql = @"delete from appInfo where num_iid = ?";
    BOOL isSuccess = [_database executeUpdate:sql,num_iid];
    if (!isSuccess) {
        NSLog(@"delete error:%@",_database.lastErrorMessage);
    }
}
-(BOOL)isExistForitem_id:(NSString *)item_id{
    NSString *sql=@"select * from appInfo where item_id= ?";
    FMResultSet *rs=[_database executeQuery:sql,item_id];
    if ([rs next]) {
        return YES;
    }else{
        return NO;
    }
}
-(NSInteger)getCounts{
    NSString *sql=@"select count(*) from appInfo";
    FMResultSet *rs=[_database executeQuery:sql];
    NSInteger count=0;
    while ([rs next]) {
        count=[[rs stringForColumnIndex:0] integerValue];
    }
    return count;
}

@end
